package cn.seecoder.courselearning.serviceimpl.question;

import cn.seecoder.courselearning.dto.question.QuestionInfoDTO;
import cn.seecoder.courselearning.dto.question.QuestionWithAnalysisDTO;
import cn.seecoder.courselearning.mapperservice.question.QuestionMapper;
import cn.seecoder.courselearning.mapperservice.test.SubmitMapper;
import cn.seecoder.courselearning.po.question.Question;
import cn.seecoder.courselearning.po.test.Submit;
import cn.seecoder.courselearning.service.question.QuestionService;
import cn.seecoder.courselearning.util.Constant;
import cn.seecoder.courselearning.vo.ResultVO;
import cn.seecoder.courselearning.vo.question.QuestionVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private SubmitMapper submitMapper;


    @Override
    public ResultVO<QuestionVO> getQuestion(Integer question_id) {
        Question question = questionMapper.selectByPrimaryKey(question_id);
        QuestionVO questionVO = new QuestionVO(question);
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取题目信息成功", questionVO);
    }

    /*
        老师在创建测试时，要根据这个方法返回的题目列表，来选择测试包含的题目。
     */
    @Override
    public List<QuestionVO> getQuestionByCourseId(Integer course_id) {
        System.out.println("QuestionServiceImpl.getQuestionByCourseId()");
        List<QuestionVO> ret = new ArrayList<>();
        List<Question> questionList = questionMapper.selectByCourse_id(course_id);
        for(Question question : questionList){
            QuestionVO questionVO = new QuestionVO(question);
            ret.add(questionVO);
        }
        return ret;
    }

    /*
        老师查询题库，该题库包含所有题目的信息。
     */
    @Override
    public List<QuestionVO> getAllquestion(){
        System.out.println("QuestionServiceImpl.getAllquestion()");
        List<QuestionVO> ret = new ArrayList<>();
        List<Question> questionList = questionMapper.selectAll();
        for(Question question : questionList){
            QuestionVO questionVO = new QuestionVO(question);
            ret.add(questionVO);
        }
        return ret;
    }


    @Override
    public ResultVO<QuestionVO> updateQuestion() {
        return null;
    }

    @Override
    public ResultVO<QuestionVO> createQuestion(QuestionVO questionVO){

        //假定不同id的题目可以有相同的题干，因此不检查重复。
        System.out.println("QuestionServiceImpl.createQuestion()");
        Question question = new Question(questionVO);
        if(questionMapper.insert(question) > 0){
            return new ResultVO<>(Constant.REQUEST_SUCCESS, "题目创建成功。", new QuestionVO(question));
        }
        else{
            return new ResultVO<>(Constant.REQUEST_FAIL, "服务器错误");
        }
    }

    @Override
    public ResultVO<QuestionInfoDTO> getQuestionToSolve(Integer question_id) {
        Question question = questionMapper.selectByPrimaryKey(question_id);
        QuestionInfoDTO questionInfoDTO = new QuestionInfoDTO(question);
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取题目信息成功", questionInfoDTO);

    }

    @Override
    public ResultVO<QuestionWithAnalysisDTO> getQuestionWithAnalysis(Integer user_id, Integer test_id, Integer question_id ) {
        Question question = questionMapper.selectByPrimaryKey(question_id);
        Submit submit = submitMapper.selectByPrimaryKey(user_id, test_id, question_id);
        if(submit==null){
            submit = new Submit();
            submit.setAns(' ');//无提交记录则将记录置为‘ ’
        }
        QuestionWithAnalysisDTO questionWithAnalysisDTO = new QuestionWithAnalysisDTO(question, submit);
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取题目信息成功", questionWithAnalysisDTO);

    }
}
